package user

import (
	"flag"
	"github.com/gin-gonic/gin"
	"google.golang.org/grpc"
	"google.golang.org/grpc/credentials/insecure"
	"log"
	"net/http"
	__ "srv/proto"
)

type List struct {
	VideoName string `form:"videoName" json:"videoName" xml:"videoName"`
	UserId    int    `form:"userId" json:"userId" xml:"userId"`
	Page      int64  `form:"page" json:"page" xml:"page"`
	PageSize  int64  `form:"pageSize" json:"pageSize" xml:"pageSize"`
}

func VideoList(c *gin.Context) {
	var json List
	if err := c.ShouldBindJSON(&json); err != nil {
		c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
		return
	}
	flag.Parse()
	// Set up a connection to the server.
	conn, err := grpc.NewClient(*addr, grpc.WithTransportCredentials(insecure.NewCredentials()))
	if err != nil {
		log.Fatalf("did not connect: %v", err)
	}
	defer conn.Close()
	con := __.NewVideoServiceClient(conn)

	// Contact the server and print out its response.
	//ctx, cancel := context.WithTimeout(context.Background(), time.Second)
	//defer cancel()
	r, err := con.VideoList(c, &__.VideoListRequest{
		VideoName: json.VideoName,
		UserId:    int64(json.UserId),
		Page:      json.Page,
		PageSize:  json.PageSize,
	})
	if err != nil {
		log.Fatalf("could not greet: %v", err)
	}
	log.Printf("Greeting: %s", r)
	c.JSON(http.StatusOK, gin.H{
		"code":    200,
		"message": "列表展示成功",
		"data":    r,
	})
}
